package com.ruoyi.project.demo.arrange.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.demo.arrange.domain.CourseArrange;
import com.ruoyi.project.demo.arrange.service.ICourseArrangeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.demo.course.service.ICourseService;

/**
 * 知识安排Controller
 *
 * @author demo
 * @date 2020-04-19
 */
@Controller
@RequestMapping("/demo/arrange")
public class CourseArrangeController extends BaseController {
    private String prefix = "demo/arrange";

    @Autowired
    private ICourseArrangeService courseArrangeService;
    @Autowired
    private ICourseService courseService;

    @RequiresPermissions("demo:arrange:view")
    @GetMapping()
    public String arrange() {
        return prefix + "/arrange";
    }

    /**
     * 查询知识安排列表
     */
    @RequiresPermissions("demo:arrange:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CourseArrange courseArrange) {
        startPage();
        List<CourseArrange> list = courseArrangeService.selectCourseArrangeList(courseArrange);
        for (int i = 0; i < list.size(); i++) {
            try {
                list.get(i).setCourse(courseService.selectCourseById(list.get(i).getCourseId()));
                list.get(i).setCourseName(list.get(i).getCourse().getName());
            } catch (Exception e) {

            }
        }
        return getDataTable(list);
    }

    /**
     * 导出知识安排列表
     */
    @RequiresPermissions("demo:arrange:export")
    @Log(title = "知识安排", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CourseArrange courseArrange) {
        List<CourseArrange> list = courseArrangeService.selectCourseArrangeList(courseArrange);
        ExcelUtil<CourseArrange> util = new ExcelUtil<CourseArrange>(CourseArrange.class);
        return util.exportExcel(list, "arrange");
    }

    /**
     * 新增知识安排
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("courses", courseService.selectCourseList(null));
        return prefix + "/add";
    }

    /**
     * 新增保存知识安排
     */
    @RequiresPermissions("demo:arrange:add")
    @Log(title = "知识安排", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CourseArrange courseArrange) {
        return toAjax(courseArrangeService.insertCourseArrange(courseArrange));
    }

    /**
     * 修改知识安排
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        CourseArrange courseArrange = courseArrangeService.selectCourseArrangeById(id);
        mmap.put("courseArrange", courseArrange);
        mmap.put("courses", courseService.selectCourseList(null));
        return prefix + "/edit";
    }

    /**
     * 修改保存知识安排
     */
    @RequiresPermissions("demo:arrange:edit")
    @Log(title = "知识安排", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CourseArrange courseArrange) {
        return toAjax(courseArrangeService.updateCourseArrange(courseArrange));
    }

    /**
     * 删除知识安排
     */
    @RequiresPermissions("demo:arrange:remove")
    @Log(title = "知识安排", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(courseArrangeService.deleteCourseArrangeByIds(ids));
    }
}
